<div class="row justify-content-center">
    <div class="col-8">
        <form (ngSubmit)="save()" [formGroup]="editForm" name="editForm" novalidate role="form">
            <h2 id="myUserLabel" jhiTranslate="userManagement.home.createOrEditLabel">
                Create or edit a User
            </h2>

            <div *ngIf="user">
                <jhi-alert-error></jhi-alert-error>

                <div [hidden]="!user.id" class="form-group">
                    <label jhiTranslate="global.field.id">ID</label>
                    <input class="form-control" formControlName="id" name="id" readonly type="text">
                </div>

                <div class="form-group">
                    <label class="form-control-label" jhiTranslate="userManagement.login">Login</label>
                    <input class="form-control" formControlName="login" name="login"
                           type="text">

                    <div *ngIf="editForm.get('login')!.invalid && (editForm.get('login')!.dirty || editForm.get('login')!.touched)">
                        <small *ngIf="editForm.get('login')?.errors?.required"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.required">
                            This field is required.
                        </small>

                        <small *ngIf="editForm.get('login')?.errors?.maxlength"
                               [translateValues]="{ max: 50 }"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.maxlength">
                            This field cannot be longer than 50 characters.
                        </small>

                        <small *ngIf="editForm.get('login')?.errors?.pattern"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.patternLogin">
                            This field can only contain letters, digits and e-mail addresses.
                        </small>
                    </div>
                </div>

                <div class="form-group">
                    <label class="form-control-label" jhiTranslate="userManagement.firstName">First Name</label>
                    <input class="form-control" formControlName="firstName" name="firstName"
                           type="text">

                    <div *ngIf="editForm.get('firstName')!.invalid && (editForm.get('firstName')!.dirty || editForm.get('firstName')!.touched)">
                        <small *ngIf="editForm.get('firstName')?.errors?.maxlength"
                               [translateValues]="{ max: 50 }"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.maxlength">
                            This field cannot be longer than 50 characters.
                        </small>
                    </div>
                </div>

                <div class="form-group">
                    <label jhiTranslate="userManagement.lastName">Last Name</label>
                    <input class="form-control" formControlName="lastName" name="lastName"
                           type="text">

                    <div *ngIf="editForm.get('lastName')!.invalid && (editForm.get('lastName')!.dirty || editForm.get('lastName')!.touched)">
                        <small *ngIf="editForm.get('lastName')?.errors?.maxlength"
                               [translateValues]="{ max: 50 }"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.maxlength">
                            This field cannot be longer than 50 characters.
                        </small>
                    </div>
                </div>

                <div class="form-group">
                    <label class="form-control-label" jhiTranslate="userManagement.email">Email</label>
                    <input class="form-control" formControlName="email" name="email" type="email">

                    <div *ngIf="editForm.get('email')!.invalid && (editForm.get('email')!.dirty || editForm.get('email')!.touched)">
                        <small *ngIf="editForm.get('email')?.errors?.required"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.required">
                            This field is required.
                        </small>

                        <small *ngIf="editForm.get('email')?.errors?.maxlength"
                               [translateValues]="{ max: 100 }"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.maxlength">
                            This field cannot be longer than 100 characters.
                        </small>

                        <small *ngIf="editForm.get('email')?.errors?.minlength"
                               [translateValues]="{ min: 5 }"
                               class="form-text text-danger"
                               jhiTranslate="entity.validation.minlength">
                            This field is required to be at least 5 characters.
                        </small>

                        <small *ngIf="editForm.get('email')?.errors?.email"
                               class="form-text text-danger"
                               jhiTranslate="global.messages.validate.email.invalid">
                            Your email is invalid.
                        </small>
                    </div>
                </div>

                <div class="form-check">
                    <label class="form-check-label" for="activated">
                        <input [attr.disabled]="user.id === undefined ? 'disabled' : null" class="form-check-input"
                               formControlName="activated" id="activated" name="activated" type="checkbox">
                        <span jhiTranslate="userManagement.activated">Activated</span>
                    </label>
                </div>

                <div *ngIf="languages && languages.length > 0" class="form-group">
                    <label jhiTranslate="userManagement.langKey">Lang Key</label>
                    <select class="form-control" formControlName="langKey" id="langKey" name="langKey">
                        <option *ngFor="let language of languages" [value]="language">{{ language | findLanguageFromKey
                            }}
                        </option>
                    </select>
                </div>

                <div class="form-group">
                    <label jhiTranslate="userManagement.profiles">Profiles</label>
                    <select class="form-control" formControlName="authorities" multiple name="authority">
                        <option *ngFor="let authority of authorities" [value]="authority">{{ authority }}</option>
                    </select>
                </div>
            </div>

            <div *ngIf="user">
                <button (click)="previousState()" class="btn btn-secondary" type="button">
                    <fa-icon icon="ban"></fa-icon>&nbsp;<span jhiTranslate="entity.action.cancel">Cancel</span>
                </button>

                <button [disabled]="editForm.invalid || isSaving" class="btn btn-primary" type="submit">
                    <fa-icon icon="save"></fa-icon>&nbsp;<span jhiTranslate="entity.action.save">Save</span>
                </button>
            </div>
        </form>
    </div>
</div>
